﻿using GenericSide;
using GM_Shop.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using GM_Shop.Model.Model;

namespace GM_Shop.DAL
{
    public class SelectData
    {
        public CNumberInfo SelectUser(CNumberInfo cNumberInfo)
        {
            var info = SqlHelper.Select<CNumberInfo>(new SqlParameter[] {
                new SqlParameter("Name",cNumberInfo.Name),
                new SqlParameter("Password",cNumberInfo.Password)
            });
            if (info.Count != 0)
                return info[0];
            else
                return null;
        }

        public List<CAnnouncement> SelectAnnouncement()
        {
            return SqlHelper.Select<CAnnouncement>("select top 4 * from Announcement order by Id desc");
        }

        public List<CNewRelease> SelectNewRelease()
        {
            return SqlHelper.Select<CNewRelease>("select top 4 * from NewRelease order by Id desc");
        }

        public List<PCommodityType> SelectCommodityType()
        {
            List<PCommodityType> list = new List<PCommodityType>();
            var type = SqlHelper.Select<CCommodityType>("select * from CommodityType");

            foreach (var item in type)
            {
                var info = new PCommodityType()
                {
                    CommodityTypeName = item.CommodityTypeName,
                    CommodityTypeListName = SqlHelper.Select<CCommodityListType>($"select * from CommodityListType where CommodityTypeId = {item.Id}").ToArray()
                };

                list.Add(info);
            }

            return list;
        }

        public List<CCommodityInfo> SelectCommodityInfo()
        {
            return SqlHelper.Select<CCommodityInfo>("select top 9 * from CommodityInfo ");
        }

        public string SelectCommodityNameById(int id)
        {
            return SqlHelper.Select<CCommodityType>(id)[0].CommodityTypeName;
        }

        public List<PCommodityInfo> SelectCommodityInfoByCount(string typeName, int count)
        {
            string sql = $"select top 5 * from CommodityInfo where Id >= ((select top 1 Id from CommodityInfo) +{count * 5})";
            //根据类型名称,先判断是否是大类别
            var date = SqlHelper.Select<CCommodityType>($"select * from CommodityType where CommodityTypeName = '{typeName}'");
            if (date.Count != 0)//大类别
            {
                //获取大类别中的小类别
                var arr = SqlHelper.Select<CCommodityListType>($"select * from CommodityListType where CommodityTypeId = {date[0].Id}");
                StringBuilder sqlChiler = new StringBuilder();
                foreach (var item in arr)
                    sqlChiler.Append(item.Id).Append(',');

                sql = sql + $" and TypeId in ( {sqlChiler.ToString().TrimEnd(',')} )";
            }
            else//小类别 
            {

            }

            var dateList = SqlHelper.Select<CCommodityInfo>(sql);

            List<PCommodityInfo> list = new List<PCommodityInfo>();

            foreach (var item in dateList)
            {
                list.Add(new PCommodityInfo()
                {
                    Id = item.Id,
                    TypeId = item.TypeId,
                    CommodityName = item.CommodityName,
                    Unit = item.Unit,
                    Quantity = item.Quantity,
                    MerchantId = item.MerchantId,
                    ProducePath = item.ProducePath,
                    IconPath = item.IconPath,
                    MerchantName = SqlHelper.Select<CMerchantInfo>(item.MerchantId)[0].Name
                });
            }
            return list;
        }

        public List<PShoppingCart> SeletcShoppingCart(int Uid)
        {
            return SqlHelper.Select<PShoppingCart>($"select A.Id,A.UId,A.CommodityInfoId,A.AddTime,A.AddCount,B.CommodityName,B.Unit from ShoppingCart A left join CommodityInfo B on A.CommodityInfoId = B.Id where UId = {Uid}");
        }

        public List<Cthink_areas> SelectProvinceList() {
            return SqlHelper.Select<Cthink_areas>("select * from think_areas where area_type = 1");
        }

        public List<Cthink_areas> SelectCityList(string city)
        {
            return SqlHelper.Select<Cthink_areas>($"select * from think_areas where parent_id = (select top 1 area_id from think_areas where area_name = '{city}')");
        }

        public CCommodityInfo SelectCommodityInfo(int id) {
            var info = SqlHelper.Select<CCommodityInfo>(id);
            if (info.Count != 0) {
                return info[0];
            }
            return null;
        }



    }
}
